/*
 * Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.gradle.unitTests.report

import com.google.gson.GsonBuilder
import org.jetbrains.kotlin.util.CompilerType
import org.jetbrains.kotlin.util.GarbageCollectionStats
import org.jetbrains.kotlin.util.PlatformType
import org.jetbrains.kotlin.util.SideStats
import org.jetbrains.kotlin.util.Time
import org.jetbrains.kotlin.util.UnitStats
import org.jetbrains.kotlin.util.UnitStatsJsonDumper
import org.junit.Test
import kotlin.test.assertEquals

class UnitStatsSerializationTest {
    @Test
    fun validateAllProperties() {
        val moduleStats = UnitStats(
            name = "all_properties",
            outputKind = null,
            platform = PlatformType.Native,
            compilerType = CompilerType.K1andK2,
            hasErrors = true,
            filesCount = 10,
            linesCount = 11,

            initStats = Time(1_000_000L, 1_000_001L, 1_000_002L),
            analysisStats = Time(2_000_000L, 2_000_001L, 2_000_002L),
            translationToIrStats = Time(3_000_000L, 3_000_001L, 3_000_002L),
            irLoweringStats = Time(4_000_000L, 4_000_001L, 4_000_002L),
            backendStats = Time(5_000_000L, 5_000_001L, 5_000_002L),
            findJavaClassStats = SideStats(1, Time(6_000_000L, 6_000_001L, 6_000_002L)),
            findKotlinClassStats = SideStats(2, Time(7_000_000L, 7_000_001L, 7_000_002L)),
            gcStats = listOf(
                GarbageCollectionStats("GC", 8_000L, 16),
                GarbageCollectionStats("GC 2", 18_000L, 4),
            ),
            jitTimeMillis = 9_000L,
        )

        val serialized = UnitStatsJsonDumper.dump(moduleStats)

        val deserialized = GsonBuilder().create().fromJson(serialized, UnitStats::class.java)

        assertEquals(moduleStats, deserialized)
    }
}